package com.ruoyi.moment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.moment.domain.SysMoment;
import com.ruoyi.moment.service.ISysMomentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 说说管理Controller
 * 
 * @author zzzyh
 * @date 2025-09-13
 */
@RestController
@RequestMapping("/moment/moment")
public class SysMomentController extends BaseController
{
    @Autowired
    private ISysMomentService sysMomentService;

    /**
     * 查询说说管理列表
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMoment sysMoment)
    {
        if (sysMoment.getUsername() != null && !sysMoment.getUsername().equals("")){
            sysMoment.setUserId(sysMomentService.getUserId(sysMoment.getUsername()));
        }
        startPage();
        List<SysMoment> list = sysMomentService.selectSysMomentList(sysMoment);
        for (SysMoment moment : list){
            moment.setUsername(sysMomentService.getUsername(moment.getUserId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出说说管理列表
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:export')")
    @Log(title = "说说管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMoment sysMoment)
    {
        List<SysMoment> list = sysMomentService.selectSysMomentList(sysMoment);
        ExcelUtil<SysMoment> util = new ExcelUtil<SysMoment>(SysMoment.class);
        util.exportExcel(response, list, "说说管理数据");
    }

    /**
     * 获取说说管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysMoment sysMoment = sysMomentService.selectSysMomentById(id);
        sysMoment.setUsername(sysMomentService.getUsername(sysMoment.getUserId()));
        return success(sysMoment);
    }

    /**
     * 新增说说管理
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:add')")
    @Log(title = "说说管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMoment sysMoment)
    {
        return toAjax(sysMomentService.insertSysMoment(sysMoment));
    }

    /**
     * 修改说说管理
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:edit')")
    @Log(title = "说说管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMoment sysMoment)
    {
        return toAjax(sysMomentService.updateSysMoment(sysMoment));
    }

    /**
     * 删除说说管理
     */
    @PreAuthorize("@ss.hasPermi('moment:moment:remove')")
    @Log(title = "说说管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysMomentService.deleteSysMomentByIds(ids));
    }
}
